package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.TeachingGroupLeaderVO;
import edu.zqu.instructionalMatters.project.entity.TeachingGroupLeader;
import edu.zqu.instructionalMatters.project.mapper.TeachingGroupLeaderMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import edu.zqu.instructionalMatters.project.service.CourseService;
import edu.zqu.instructionalMatters.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 课程组组长 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "课程组组长")
public class TeachingGroupLeaderController {
    @Autowired
    private TeachingGroupLeaderMapper teachingGroupLeaderMapper;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取课程组组长列表")
    @RequestMapping(value = "/teachingGroupLeaders", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             BigInteger courseId,
                             BigInteger teacherId
    ) {
        TeachingGroupLeader teachingGroupLeader1 = new TeachingGroupLeader()
                .setCollegeId(collegeId).setCourseId(courseId).setTeacherId(teacherId);                                 //合成对象
        List<TeachingGroupLeader> list1 = teachingGroupLeaderMapper.findAll(teachingGroupLeader1);                      //获取 TeachingGroupLeader 集合
        List<TeachingGroupLeaderVO> list = new ArrayList<>();                                                           //初始化 TeachingGroupLeaderVO 集合
        for (TeachingGroupLeader teachingGroupLeader : list1) {                                                          //循环遍历
            TeachingGroupLeaderVO teachingGroupLeaderVO = new TeachingGroupLeaderVO();                                  //初始化 TeachingGroupLeaderVO
            BeanUtils.copyProperties(teachingGroupLeader, teachingGroupLeaderVO);                                        //对象复制赋值
            String collegeName = collegeService.getCollegeName(teachingGroupLeader.getCollegeId());                     //获取学校名称
            String courseName = courseService.getCourseName(teachingGroupLeader.getCourseId());                         //获取课程名称
            String teacherName = userService.getUserName(teachingGroupLeader.getTeacherId());                           //获取教师名称
            teachingGroupLeaderVO.setCollegeName(collegeName);                                                          //设置学校名称
            teachingGroupLeaderVO.setCourseName(courseName);                                                            //设置课程名称
            teachingGroupLeaderVO.setTeacherName(teacherName);                                                          //设置教师名称
            list.add(teachingGroupLeaderVO);                                                                            //加入 list
        }
        return ZQUResult.success(list);                                                                                 //返回 list
    }

    @ApiOperation(value = "获取课程组组长信息", response = TeachingGroupLeaderVO.class)
    @GetMapping(value = "/teachingGroupLeader/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        TeachingGroupLeader teachingGroupLeader = teachingGroupLeaderMapper.selectById(id);         //查询数据对象
        TeachingGroupLeaderVO teachingGroupLeaderVO = new TeachingGroupLeaderVO();                  //初始化VO
        BeanUtils.copyProperties(teachingGroupLeader, teachingGroupLeaderVO);                        //对象复制赋值
        String collegeName = collegeService.getCollegeName(teachingGroupLeader.getCollegeId());     //获取学校名称
        String courseName = courseService.getCourseName(teachingGroupLeader.getCourseId());         //获取课程名称
        String teacherName = userService.getUserName(teachingGroupLeader.getTeacherId());           //获取教师名称
        teachingGroupLeaderVO.setCollegeName(collegeName);                                          //设置学校名称
        teachingGroupLeaderVO.setCourseName(courseName);                                            //设置课程名称
        teachingGroupLeaderVO.setTeacherName(teacherName);                                          //设置教师名称
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", teachingGroupLeaderVO);           //成功返回数据对象
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/teachingGroupLeader", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "beginTime", value = "开始时间", paramType = "query", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger teacherId,
                         BigInteger courseId,
                         Date beginTime,
                         Date endTime,
                         BigInteger byWho) {
        TeachingGroupLeader teachingGroupLeader = new TeachingGroupLeader();               //初始化
        teachingGroupLeader.setCollegeId(collegeId);                                       //设置学校id
        teachingGroupLeader.setTeacherId(teacherId);                                       //设置教师id
        teachingGroupLeader.setCourseId(courseId);                                         //设置课程id
        teachingGroupLeader.setBeginTime(beginTime);                                       //设置开始担任课程组长的时间
        teachingGroupLeader.setEndTime(endTime);                                           //设置结束担任课程组长的时间
        teachingGroupLeader.setByWho(byWho);                                               //设置操作者
        teachingGroupLeaderMapper.insert(teachingGroupLeader);                             //执行插入
        return ZQUResult.success();                                                        //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/teachingGroupLeader/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "beginTime", value = "开始时间", paramType = "query", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger teacherId,
                            BigInteger courseId,
                            Date beginTime,
                            Date endTime,
                            BigInteger byWho) {
        TeachingGroupLeader teachingGroupLeader = teachingGroupLeaderMapper.selectById(id);//先查出旧纪录
        teachingGroupLeader.setByWho(byWho);                                               //修改操作者
        teachingGroupLeaderMapper.updateById(teachingGroupLeader);                         //更新操作者
        teachingGroupLeaderMapper.deleteById(id);                                          //逻辑删除
        teachingGroupLeader = new TeachingGroupLeader();                                   //初始化新记录
        teachingGroupLeader.setCollegeId(collegeId);                                       //设置学校id
        teachingGroupLeader.setTeacherId(teacherId);                                       //设置教师id
        teachingGroupLeader.setCourseId(courseId);                                         //设置课程id
        teachingGroupLeader.setBeginTime(beginTime);                                       //设置开始担任课程组长的时间
        teachingGroupLeader.setEndTime(endTime);                                           //设置结束担任课程组长的时间
        teachingGroupLeader.setByWho(byWho);                                               //设置操作者
        teachingGroupLeader.setLastVersion(id);                                            //设置上一版本
        teachingGroupLeaderMapper.insert(teachingGroupLeader);                             //执行插入
        return ZQUResult.success();                                                        //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/teachingGroupLeader/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        TeachingGroupLeader teachingGroupLeader = teachingGroupLeaderMapper.selectById(id);//先查出旧纪录
        teachingGroupLeader.setByWho(byWho);                                               //修改操作者
        teachingGroupLeaderMapper.updateById(teachingGroupLeader);                         //更新操作者
        teachingGroupLeaderMapper.deleteById(id);                                          //逻辑删除
        return ZQUResult.success();                                                        //操作成功返回
    }
}

